package net.feling.mcloud.filter;

import net.feling.mcloud.entity.Message;
import net.feling.mcloud.entity.User;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

public class UserDecoder extends CumulativeProtocolDecoder {
    private Charset charset;

    public UserDecoder(Charset charset) {
        this.charset = charset;
    }

    @Override
    protected boolean doDecode(IoSession ioSession, IoBuffer ioBuffer, ProtocolDecoderOutput output) throws Exception {
        CharsetDecoder charsetDecoder = charset.newDecoder();
        IoBuffer buffer = IoBuffer.allocate(100);
        buffer.setAutoExpand(true);

        Message message = new Message();
        String type = "";
        String sender = null;
        User user = null;

        int textLineNumber = 1;
        int columnNumber = 0;

        while (ioBuffer.hasRemaining()) {
            byte bt = ioBuffer.get();
            buffer.put(bt);

            if (type.equals(User.getType()) && sender != null) {
                if (user == null) {
                    user = new User();
                }
                if (bt == '\n' && textLineNumber < 8) {
                    columnNumber++;
                    if (textLineNumber == 3) {
                        buffer.flip();
                        String uid = buffer.getString(columnNumber, charsetDecoder);
                        uid = uid.substring(0, uid.length() - 1);
                        columnNumber = 0;
                        buffer.clear();
                        user.setUid(uid);
                        System.out.println("uid = " + uid);
                    }
                    if (textLineNumber == 4) {
                        buffer.flip();
                        String CPUfreq = buffer.getString(columnNumber, charsetDecoder);
                        CPUfreq = CPUfreq.substring(0, CPUfreq.length() - 1);
                        columnNumber = 0;
                        buffer.clear();
                        user.setCPUfreq(CPUfreq);
                        System.out.println("CPUfreq = " + CPUfreq);
                    }
                    if (textLineNumber == 5) {
                        buffer.flip();
                        String memory = buffer.getString(columnNumber, charsetDecoder);
                        memory = memory.substring(0, memory.length() - 1);
                        columnNumber = 0;
                        buffer.clear();
                        user.setMemory(memory);
                        System.out.println("memory = " + memory);
                    }
                    if (textLineNumber == 6) {
                        buffer.flip();
                        String batteryLeft = buffer.getString(columnNumber, charsetDecoder);
                        batteryLeft = batteryLeft.substring(0, batteryLeft.length() - 1);
                        columnNumber = 0;
                        buffer.clear();
                        user.setBatteryLeft(batteryLeft);
                        System.out.println("batteryLeft = " + batteryLeft);
                    }
                    if (textLineNumber == 7) {
                        buffer.flip();
                        String GPSInfo = buffer.getString(columnNumber, charsetDecoder);
                        GPSInfo = GPSInfo.substring(0, GPSInfo.length() - 1);
                        columnNumber = 0;
                        buffer.clear();
                        user.setGPSInfo(GPSInfo);
                        System.out.println("GPSInfo = " + GPSInfo);
                    }
                    textLineNumber++;
                    if (textLineNumber > 7) {
                        message.setContent(user);
                    }
                } else {
                    columnNumber++;
                }
            } else if (type.equals("".getClass().getSimpleName()) && sender != null) {
                if (bt == '\n' && textLineNumber < 4) {
                    columnNumber++;
                    if (textLineNumber == 3) {
                        buffer.flip();
                        String content = buffer.getString(columnNumber, charsetDecoder);
                        content = content.substring(0, content.length() - 1);
                        columnNumber = 0;
                        buffer.clear();
                        message.setContent(content);
                        System.out.println("content = " + content);
                    }
                    textLineNumber++;
                }else columnNumber++;
            } else {

                if (bt == '\n' && textLineNumber < 3) {
                    columnNumber++;
                    if (textLineNumber == 1) {
                        buffer.flip();
                        type = buffer.getString(columnNumber, charsetDecoder);
                        type = type.substring(0, type.length() - 1);
                        columnNumber = 0;
                        buffer.clear();
                        System.out.println("type = " + type);
                    }
                    if (textLineNumber == 2) {
                        buffer.flip();
                        sender = buffer.getString(columnNumber, charsetDecoder);
                        sender = sender.substring(0, sender.length() - 1);
                        columnNumber = 0;
                        buffer.clear();
                        System.out.println("sender = " + sender);

                    }
                    textLineNumber++;
                    if (textLineNumber > 2) {
                        message.setType(type);
                        message.setSender(sender);
                        System.out.println("message的type和sender封装完毕");
                    }
                } else {
                    columnNumber++;
                }
            }
        }

        output.write(message);
        System.out.println("decode成功");
        return false;
    }
}